<?php

/**
 * StaffEducationBackground form.
 *
 * @package    erp-dnict
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class StaffEducationBackgroundForm extends BaseStaffEducationBackgroundForm
{
  public function configure()
  {
  	$this->setWidgets(array(
        'education_type'                =>  new sfWidgetFormSelect(array('choices' => $this->getEducationTypes())),
        'education_background_id'       =>  new sfWidgetFormSelect(array('choices' => $this->getDefaultEducationIds())),
        'grade'           				=>  new sfWidgetFormInput(),
        'issue_date'	  				=>  new sfWidgetFormInput(),
        'issue_establishment'			=> 	new sfWidgetFormInput(),
        ));

    $this->widgetSchema->setLabels(array(
        'education_type'                =>  'Loại hình:',
        'education_background_id'       =>  'Tên bằng cấp:',
        'grade'            				=>  'Tốt nghiệp loại:',
        'issue_date'           			=>  'Ngày nhận bằng:',
        'issue_establishment'           =>  'Đơn vị cấp bằng:',
        ));

    $this->setValidators(array(
        'education_type'                => new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getEducationTypes()))),
        'education_background_id'       => new sfValidatorChoice(array('required' => true, 'choices' => array_keys($this->getDefaultEducationIds()))),
        'grade'            				=> new sfValidatorString(array('required' => true)),
        'issue_date'           			=> new sfValidatorDate(array('required' => true, 'date_output' => 'dd/mm/yy', 'date_format' => "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/")),
        'issue_establishment'           => new sfValidatorString(array('required' => true)),
        ));
    $this->widgetSchema->setNameFormat('staff_profile[%s]');
  }

  private function getEducationTypes()
  {
  	return array(
			EducationBackground::TYPE_EDUCATION => 'Học vấn',
			EducationBackground::TYPE_POLITIC => 'Chính trị',
			EducationBackground::TYPE_LANGUAGE => 'Ngoại ngữ,'
					);
  }

  private function getDefaultEducationIds()
  {
  	$items = array();
  	$q = Doctrine_Query::create()
  			->from('EducationBackground ed')
  			->addWhere('ed.is_active = ?', true)
  			->addWhere('ed.education_type = ?', EducationBackground::TYPE_EDUCATION);

  	$results = $q->execute();
  	foreach ($results as $result)
  		$items[$result['id']] = $result['name'];

  	return $items;
  }
}
